package org.eclipse.equinox.internal.autostart;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.*;

/*******************************************************************************
* Copyright (c) 2008 Paul Vanderlei, Simon Archer, Jeff McAffer and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Toast is an Equinox/OSGi system developed for the book
* Eclipse, Equinox and OSGi -
* Creating Highly Modular Java Applications
* See http://equinoxosgi.org
*
* Contributors:
* Paul Vanderlei, Simon Archer and Jeff McAffer - initial API and implementation

*******************************************************************************/
public class Activator implements BundleActivator, BundleListener {


	public void start(BundleContext context) throws Exception {
		context.addBundleListener(this);
		Bundle[] bundles = context.getBundles();
		for (int i = 0; i < bundles.length; i++)
			start(bundles[i]);
	}

	private void start(Bundle bundle) {
		boolean isFragment = bundle.getHeaders().get(Constants.FRAGMENT_HOST) != null;
		if (!isFragment && bundle.getState() == Bundle.RESOLVED)
			try {
			bundle.start();
			} catch (BundleException e) {
			// TODO do nothing for now
				e.printStackTrace();
			}
	}

	public void stop(BundleContext context) throws Exception {
		context.removeBundleListener(this);
	}

	public void bundleChanged(BundleEvent event) {		
		if (event.getType() == BundleEvent.RESOLVED)
			start(event.getBundle());
	}
}